package com.ruoyi.aitcommon.utils;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aitcommon.config.LocalDBConfig;
import com.ruoyi.aitcommon.vo.DBConVO;
import com.ruoyi.common.utils.StringUtils;

/**
 * 版本1 未使用连接池,废弃
 * conn示例:
 * sqlserver:"jdbc:sqlserver://localhost:1433;databasename=XX";
 * oracle:"jdbc:oracle:thin:@localhost:1521:orcl"
 * mysql:"jdbc:mysql://localhost:3306/sqltestdb?serverTimezone=Asia/Shanghai"
 * 
 * @author Administrator
 */
public class DBUtils1 {
	public String driveClassName="";
	public String url="";
	public String user="";
	public String password="";
	
	public ResultSet retset = null;
	public Connection connect = null;
	public PreparedStatement preState = null;
	public DBConVO dbCon=null;
	public Statement state = null;
//	数据库驱动
	private static final String SQLSERVER_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private static final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
	private static final String MYSQL_DRIVER = "com.mysql.cj.jdbc.Driver";
	
	@Override
	public String toString() {
		return "DBUtils [driveClassName=" + driveClassName + ", url=" + url + ", user=" + user + ", password="
				+ password + "]";
	}
	
//	链接配置文件上的数据库
	public DBUtils1() throws Exception {
		this("", "", "");
	}
	
//	指定数据库链接信息
	public DBUtils1(String url, String user, String password) throws Exception {
		if (StringUtils.isBlank(url)) {
//			DBConfig为Spring配置类，通过静态化，可以在Utils类中直接使用
			url = LocalDBConfig.getUrl();
		}
		if (StringUtils.isBlank(user)) {
			user = LocalDBConfig.getUsername();
		}
		if (StringUtils.isBlank(password)) {
			password = LocalDBConfig.getPassword();
		}
		this.url=url;
		this.user=user;
		this.password=password;
		this.dbCon=new DBConVO();
		this.dbCon.setUrl(url);
		this.dbCon.setUsername(user);
		this.dbCon.setPassword(password);
		
		// 第一步：注册驱动
		if (url.contains("sqlserver")) {
			Class.forName(SQLSERVER_DRIVER);
			this.driveClassName=SQLSERVER_DRIVER;
		} else if (url.contains("oracle")) {
			Class.forName(ORACLE_DRIVER);
			this.driveClassName=ORACLE_DRIVER;
		} else if (url.contains("mysql")) {
			Class.forName(MYSQL_DRIVER);
			this.driveClassName=MYSQL_DRIVER;
		}
	}
	
	public DBConVO getDBConVO() {
		return this.dbCon;
	}
	public JSONObject getDBConJSON() {
		JSONObject json=(JSONObject) JSONObject.toJSON(this.dbCon);
		return json;
	}
	
	public Connection open() throws Exception {
		// 第二步：获取连接
		this.connect = DriverManager.getConnection(this.url, this.user, this.password);
		return this.connect;
	}

	public PreparedStatement getPreState(String sql) throws Exception {
		if(this.connect==null)
			this.open();
		this.preState=connect.prepareStatement(sql);
		return this.preState;
	}
	public Statement getStatement() throws Exception {
		if(this.connect==null)
			this.open();
		this.state=connect.createStatement();
		return this.state;
	}
	
	public CallableStatement getPreCall(String sql) throws Exception {
		if(this.connect==null)
			this.open();
		this.state=connect.prepareCall(sql);
		return (CallableStatement)this.state;
	}
	
	public void close() throws Exception {
		// 第六步：关闭资源
		if (retset != null)
			retset.close();
		if (preState != null)
			preState.close();
		if (state != null)
			state.close();
		if (connect != null)
			connect.close();
	}

	protected void finalize() throws Exception {
		this.close();
	}
}
